// pages/SCM/store/goods/goods.js
const app = getApp()
const util = require('../../../../utils/util.js')
const lbbService = require('../../../service/service.js')

Page({

  /**
   * 页面的初始数据
   */
  data: {
    goods:[],
    categories:[]
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.setData({
      scrollHeight: app.getScrollHeight(112)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    let that = this
    that.toast = that.selectComponent("#toast")
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    let that = this
    that.getGoodsCategoryList();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  toggleTab:function(e){
      let id=e.currentTarget.dataset.id;
          if (id == this.data.tabNum) {
                  id = '';
          }
      this.setData({tabNum:id});
      this.getGoodsList();
  },
  toItem: function (e) {
    console.log('toDetail>>', e)
    let id = e.currentTarget.dataset.id
    wx.navigateTo({
      url: '../goodsAdd/goodsAdd?id=' + id,
    })
  },
  getGoodsList: function(){
    let that = this
    let params={};
    let tabNum=that.data.tabNum;
    if(tabNum){
        params.categoryId=tabNum;
    }
    lbbService.getGoodsList(params, function (res) {
      that.setData({
        goods: res.data
      })
    })
  },
  getGoodsCategoryList:function(){
    let that=this;
    lbbService.getGoodsCategoryList({}, function (res) {
      //let tabNum=res.data[0].id;
        let  tabNum = '';
      that.setData({
        categories: res.data,
        tabNum:tabNum,
      });
      that.getGoodsList();
    })
  },
  saveItem: function(){
    let that=this;
    let name=that.data.name;
    let sortNum=that.data.sortNum;
    if (!name) {
      that.toast.showToast('请输入仓库名称');
      return;
    }
    sortNum = parseInt(sortNum);
    let params={name:name};
    if (!isNaN(sortNum)){
        params.sortNum=sortNum;
    }
    lbbService.saveWarehouse(params, function (res) {
      that.setData({ hideFlag: true });
      wx.showToast({
        title: '增加成功',
      })
      that.getList();
    });
  },
  
  addItem: function(e){
    wx.navigateTo({
      url: '../goodsAdd/goodsAdd',
    })
  },
  hideModal:function(){
    this.setData({hideFlag:true});
  },
  stopMaoPao:function(){
    return true;
  },


  formSubmit: function (e) {
    console.log(e.detail.value);
    let name = e.detail.value.name;
    let sortNum = e.detail.value.sortNum;
    sortNum = sortNum ? parseInt(sortNum) : 0;
    if (!name) {
      this.toast.showToast('请输入仓库名称');
      return;
    }
    let that = this;
    lbbService.saveWarehouse({ name: name, sortNum: sortNum }, function (res) {
      wx.showToast({
        title: '增加成功',
      })
      that.getList();
    });
  },
  
})